package com.sea.data.controller;


import cn.hutool.core.util.CharsetUtil;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import cn.hutool.crypto.symmetric.SymmetricCrypto;
import cn.hutool.json.JSONUtil;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;


import java.util.*;


public class TestController {

    public static String TEST_BODY ="";
    //        String AES_PWD = "iJiEdHPICP8JYL49GjiXSQ==";
    public static String AES_PWD = "x/QkOLAsrVfuVCYZ8SX/Wg==";
    public static void main(String[] args) {
        // 示例Base64编码字符串


        Map<String, String> request = new HashMap<>();

        request.put("appId", "API_Test");
        //request.put("appId", "836a2e67b11d4490889f5e5e8466bc0c");
        request.put("uniqueNo", UUID.randomUUID().toString());
        request.put("timezoneOffset", "8");
        request.put("timestamp", String.valueOf(System.currentTimeMillis()));
        //加密json字符串要通过代码转换，不要携带\n换行符号
        String replace = TEST_BODY.replace("\n", "");
        System.out.println(replace);
        request.put("body", replace);

        //1.2拼装签名字符串
        String originSign1 = "appId=" + request.get("appId") + "&uniqueNo=" + request.get("uniqueNo") + "&timezoneOffset=" + request.get("timezoneOffset") + "&timestamp=" + request.get("timestamp");
        System.out.println("原始签名:" + originSign1);
        //1.3加密
        System.out.println("原始body:" + request.get("body"));
        String encryptBody = aesEncrypt(request.get("body"), AES_PWD);
        System.out.println("加密body:" + encryptBody);
        request.put("body", encryptBody);

        System.out.println(JSONUtil.toJsonPrettyStr(request));
        System.out.println("模拟 http post request  >>>> ");

        //模拟无忧达接收请求解密
        // 注意版本兼容，demo使用的是 hutool-all-5.4.4
        String decryptBody = aesDecrypt(encryptBody, AES_PWD);
        System.out.println("解密后的数据：\n" + decryptBody);
        postTest(request);
    }
    public static void postTest(Map<String,String> map) {
        String url = "https://oms-openapi-test.wydgroup.com//oms//openapi/common/v1/getWarehouseByCustomer";
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        try {
            StringEntity entity = new StringEntity(JSONUtil.toJsonPrettyStr(map), "UTF-8");
            entity.setContentType("application/json");
            httpPost.setEntity(entity);
            HttpResponse response = httpClient.execute(httpPost);
            String responseBody = EntityUtils.toString(response.getEntity(),"UTF-8");
            System.out.println(responseBody);
            String data = JSONUtil.parseObj(responseBody).getStr("data");
            String decryptBody = aesDecrypt(data, AES_PWD);
            System.out.println("解密后的数据：\n" + decryptBody);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static String aesEncrypt(String content, String keyString) {
        byte[] key = Base64.decodeBase64(keyString);
        //构建
        SymmetricCrypto aes = new SymmetricCrypto(SymmetricAlgorithm.AES, key);
        //加密
        byte[] encrypt = aes.encrypt(content);
        String encryptString = Base64.encodeBase64String(encrypt);
        System.out.println("加密后的数据：\n" + encryptString);
        return encryptString;
    }
    private static String aesDecrypt(String body, String keyString) {
        byte[] key = Base64.decodeBase64(keyString);
        //构建
        SymmetricCrypto aes = new SymmetricCrypto(SymmetricAlgorithm.AES, key);
        //解密
        byte[] decrypt = aes.decrypt(body);
        return new String(decrypt, CharsetUtil.CHARSET_UTF_8);
    }


}
